package com.drought.rained.ui.activity.mine;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.bean.BindedCardDataLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.mvpview.BankCardMvpViewLJ;
import com.drought.rained.presenter.BankCardPresenterLJ;
import com.drought.rained.response.AuthInfoResponseLJ;
import com.drought.rained.response.BindedCardResponseLJ;
import com.drought.rained.response.BlankResponseLJ;
import com.drought.rained.response.PersonalInfoResponseLJ;
import com.drought.rained.utils.StringUtilsLJ;

import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(name = "银行卡", path = RouterMapLJ.MINE_BANK_CARD)
public class MineBankCardActivityLJ extends BaseTitleActivity implements BankCardMvpViewLJ {

    private LinearLayout bankCardEmptyLayout;
    private TextView tvStatusContent;
    private Button btnBind;
    private LinearLayout bankCardLayout;
    private TextView tvBankName;
    private TextView tvRealName;
    private TextView tvCardNumber;
    private TextView tvTips;
    private BankCardPresenterLJ presenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        bankCardEmptyLayout = findViewById(R.id.layout_bank_card_empty);
        tvStatusContent = findViewById(R.id.tv_status_content);
        btnBind = findViewById(R.id.btn_bind);
        bankCardLayout = findViewById(R.id.layout_bank_card);
        tvBankName = findViewById(R.id.tv_bank_name);
        tvRealName = findViewById(R.id.tv_real_name);
        tvCardNumber = findViewById(R.id.tv_card_number);
        tvTips = findViewById(R.id.tv_tips);
    }
    @OnClick(R.id.btn_bind)
    public void onClick() {
        ARouter.getInstance().build(RouterMapLJ.BANK_AUTH_PAGE).withBoolean("is_change", true).navigation();
    }

    @Override
    protected void onUIReady() {
        presenter = new BankCardPresenterLJ(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (presenter != null) {
            presenter.getBankInfo();
            showLoadingView();
        }
    }

    private void fillCardView(BindedCardDataLJ data) {
        dismissLoadingView();

        if ("1".equals(data.getBankStatus())) {
            showNoDataView();

            String statusContent, btnContent, tips;// 1为换卡
            statusContent = getStringResource(R.string.mine_bank_card_failure_hint);
            btnContent = getStringResource(R.string.mine_bank_card_failure_btn);
            tips = getStringResource(R.string.mine_bank_card_failure_tip);

            if (tvStatusContent != null) {
                tvStatusContent.setText(statusContent);
            }
            if (btnBind != null) {
                btnBind.setVisibility(View.VISIBLE);
                btnBind.setText(btnContent);
            }
            if (tvTips != null) {
                tvTips.setVisibility(View.VISIBLE);
                tvTips.setText(tips);
            }
        } else {
            if (StringUtilsLJ.isEmpty(data.getBankCard())) {
                showNoDataView();
                String tips = getStringResource(R.string.mine_bank_card_empty_tip);

                if (btnBind != null) {
                    btnBind.setVisibility(View.GONE);
                }
                if (tvTips != null) {
                    tvTips.setVisibility(View.VISIBLE);
                    tvTips.setText(tips);
                }
            } else {
                showNormalView();
                if (tvRealName != null) {
                    tvRealName.setText(StringUtilsLJ.getMaskCodeStr(data.getRealName(), 3, 4));
                }
                if (tvBankName != null) {
                    tvBankName.setText(data.getBankValue());
                }
                if (tvCardNumber != null) {
                    tvCardNumber.setText(StringUtilsLJ.getMaskCodeStr(data.getBankCard(), 3, 4));
                }
                tvTips.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.mine_bank_card_page_title);
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_mine_bank_card;
    }

    private void showNormalView() {
        bankCardLayout.setVisibility(View.VISIBLE);
        bankCardEmptyLayout.setVisibility(View.GONE);
    }

    private void showNoDataView() {
        bankCardEmptyLayout.setVisibility(View.VISIBLE);
        bankCardLayout.setVisibility(View.GONE);

    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }

    @Override
    public void onGetDataNext(BindedCardResponseLJ response) {
        fillCardView(response.getData());
    }

    @Override
    public void onGetDataNext(BlankResponseLJ response) {}

    @Override
    public void onGetDataNext(AuthInfoResponseLJ response) {}

    @Override
    public void onGetDataNext(PersonalInfoResponseLJ response) {}
}
